package com.ruoyi.zhgd.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.zhgd.util.Query;
import com.ruoyi.zhgd.domain.CollectPiece;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.CollectPieceVo;
import com.ruoyi.zhgd.domain.bo.CollectPieceBo;
import com.ruoyi.zhgd.service.ICollectPieceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 盘信息
 *
 * @author yhy
 * @date 2023-09-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/piece")
public class CollectPieceController extends BaseController {

    private final ICollectPieceService iCollectPieceService;

    /**
     * 查询盘信息列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<CollectPieceVo> list(CollectPieceBo bo, PageQuery pageQuery) {
        return iCollectPieceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出盘信息列表
     */
    @SaCheckLogin
    @Log(title = "盘信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CollectPieceBo bo, HttpServletResponse response) {
        List<CollectPieceVo> list = iCollectPieceService.queryList(bo);
        ExcelUtil.exportExcel(list, "盘信息", CollectPieceVo.class, response);
    }

    /**
     * 获取盘信息详细信息
     *
     * @param pkId 主键
     */
    @SaCheckLogin
    @GetMapping("/{pkId}")
    public R<CollectPieceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long pkId) {
        return R.ok(iCollectPieceService.queryById(pkId));
    }

    /**
     * 新增盘信息
     */
    @SaCheckLogin
    @Log(title = "盘信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CollectPiece bo) {
        return toAjax(iCollectPieceService.insertByBo(bo));
    }

    /**
     * 修改盘信息
     */
    @SaCheckLogin
    @Log(title = "盘信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CollectPieceBo bo) {
        return toAjax(iCollectPieceService.updateByBo(bo));
    }

    /**
     * 删除盘信息
     *
     * @param pkIds 主键串
     */
    @SaCheckLogin
    @Log(title = "盘信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pkIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] pkIds) {
        return toAjax(iCollectPieceService.deleteWithValidByIds(Arrays.asList(pkIds), true));
    }


    @SaCheckLogin
    @GetMapping("/pieceList")
    public R pieceList(@RequestParam(value = "pk_id" ,required = false) String pk_id,
        @RequestParam(value = "pageNum" , required = false) Integer pageNum,
        @RequestParam(value = "pageSize" , required = false) Integer pageSize){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page",pageNum);
        paramMap.put("limit",pageSize);
        PageUtils pageUtils = iCollectPieceService.pieceList(new Query(paramMap).getPage(), pk_id);
        return R.ok(pageUtils);
    }


}
